﻿//-----------------------------------------------------------------------
// <copyright file="GenericSettingsProvider.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>02/05/2013</date>
//-----------------------------------------------------------------------

using ConsoleHoster.Common.Utilities;
using ConsoleHoster.Common.Utilities.Threading;
using System.Windows;

namespace ConsoleHoster.Common.Model
{
	public abstract class GenericSettingsProvider<T, V, U> : UIBasedSettingsProvider<T>, IProvider<V, U> where T : Window
	{
		public GenericSettingsProvider(IDispatcher argDispatcher, ViewSettings argViewSettings, ILogger argLogger)
			: base(argDispatcher, argViewSettings, argLogger)
		{
		}

		public V ProvideData(U argCurrentState)
		{
			V tmpResult = default(V);

			this.view = this.CreateView(argCurrentState);
			this.view.WindowStartupLocation = WindowStartupLocation.CenterOwner;
			this.view.Owner = this.ViewSettings.OwnerWindow;
			this.view.Style = this.ViewSettings.DefaultWindowStyle;

			bool? tmpDialogResult = this.view.ShowDialog();
			if (tmpDialogResult.HasValue && tmpDialogResult.Value)
			{
				tmpResult = this.RetrieveDataFromView();
			}
			this.view = null;

			return tmpResult;
		}

		protected abstract T CreateView(U argCurrentState);

		protected abstract V RetrieveDataFromView();
	}
}
